﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
using GameEngine.Libs;

namespace GameEngine.Model.Data
{
    public class TileVO : ITileVO
    {
        public Point Position { get; set; }
        public float Height { get; set; }

        /// <summary>
        /// If occupied, troops can't pass through it
        /// </summary>
        public bool Occupied { get; set; }

        /// <summary>
        /// If destructible Occupied can be passed from true to false
        /// </summary>
        public bool Destructible { get; set; }

        /// <summary>
        /// Type of terrain, water, wood,
        /// </summary>
        public int Type { get; set; }

        public TileVO()
        {
            Position = new Point() { X = 0, Y = 0 };
            Height = 0;
            Occupied = false;
            Destructible = false;
            Type = 0;
        }

        public virtual string Serialize()
        {
            //return Serializer.Serialize(VO);
            XDocument doc = new XDocument();
            XElement mainNode = new XElement("TileVO");
            {
                XElement node = new XElement("Position");
                node.Add(new XCData(Serializer.Serialize(Position)));
                mainNode.Add(node);
            }
            {
                XElement node = new XElement("Height");
                node.Add(new XCData(Serializer.Serialize(Height)));
                mainNode.Add(node);
            }
            {
                XElement node = new XElement("Occupied");
                node.Add(new XCData(Serializer.Serialize(Occupied)));
                mainNode.Add(node);
            }
            {
                XElement node = new XElement("Destructible");
                node.Add(new XCData(Serializer.Serialize(Destructible)));
                mainNode.Add(node);
            }
            {
                XElement node = new XElement("Type");
                node.Add(new XCData(Serializer.Serialize(Type)));
                mainNode.Add(node);
            }
            return mainNode.ToString();
        }

        public virtual void DeSerialize(string serializedString)
        {
            XElement mainNode = XElement.Parse(serializedString);
            foreach (XElement node in mainNode.Nodes())
            {
                switch (node.Name.LocalName)
                {
                    case "Position":
                        {
                            Position = (Point)Serializer.DeSerialize(typeof(Point), node.Value);
                        }
                        break;
                    case "Height":
                        {
                            Height = (float)Serializer.DeSerialize(typeof(float), node.Value);
                        }
                        break;
                    case "Occupied":
                        {
                            Occupied = (bool)Serializer.DeSerialize(typeof(bool), node.Value);
                        }
                        break;
                    case "Destructible":
                        {
                            Destructible = (bool)Serializer.DeSerialize(typeof(bool), node.Value);
                        }
                        break;
                    case "Type":
                        {
                            Type = (int)Serializer.DeSerialize(typeof(int), node.Value);
                        }
                        break;
                }
            }
        }
    }
}
